package llutyo.andcrypt;

/*Copyright 2012 Akos Ludanyi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SetPasswordActivity extends Activity {
	public final static String EXTRA_PASSWORD = "AndCrypt.EXTRA_PASSWORD";
	
	private EditText inp, inp2;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.pwd);
		
		inp = (EditText)findViewById(R.id.pwd_inp);
		inp2 = (EditText)findViewById(R.id.pwd_inp2);
		
		Button ok = (Button)findViewById(R.id.pwd_ok);
		ok.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				onOkClicked();		
			}
		});
		
		setResult(RESULT_CANCELED);
	}
	
	private void onOkClicked() {
		String p1 = inp.getText().toString();
		String p2 = inp2.getText().toString();
		
		if(p1.length() == 0) {
			AlertDialog.Builder d = new AlertDialog.Builder(this);
			d.setMessage("Password cannot be empty!");
			d.setPositiveButton("Ok", null);
			d.show();
			return;
		}
		
		if(!p1.equals(p2)) {
			AlertDialog.Builder d = new AlertDialog.Builder(this);
			d.setMessage("Passwords do not match!");
			d.setPositiveButton("Ok", null);
			d.show();
			return;
		}
		
		Intent intent = new Intent();
		intent.putExtra(EXTRA_PASSWORD, p1);
		setResult(RESULT_OK, intent);
		finish();
	}
}
